/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice.persistence.messagebean;

import org.proteusframework.core.base.Namespace;
import org.proteusframework.core.util.Assert;
import org.proteusframework.platformservice.persistence.api.messagebean.IMessageBeanIndex;
import org.proteusframework.platformservice.persistence.api.messagebean.IMessageBeanProperty;

import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Package private {@link IMessageBeanIndex} concrete implementation. Developer must use the
 * {@link MessageBeanBuilder} to create instances of {@link IMessageBeanIndex}.
 *
 * @author Tacoma Four
 */
final class MessageBeanIndex extends Namespace implements IMessageBeanIndex
{
    private final boolean isUnique;
    private SortedSet<IMessageBeanProperty> fields = new TreeSet<IMessageBeanProperty>();

    MessageBeanIndex(String family, String name, boolean isUnique, IMessageBeanProperty... messageField)
    {
        super(family, name);
        this.isUnique = isUnique;

        Assert.parameterNotNull(messageField,
                "Parameter 'messageField' must not be null and must include at least one field");

        fields.addAll(Arrays.asList(messageField));
    }

    @Override
    public boolean isUnique()
    {
        return isUnique;
    }

    @Override
    public SortedSet<IMessageBeanProperty> getIndex()
    {
        return fields;
    }

    @Override
    public String toString()
    {
        return "MessageBeanIndex{" +
                "isUnique=" + isUnique +
                ", fields=" + fields +
                "} " + super.toString();
    }
}
